<template>
	<view class="index">

		<!-- #ifdef APP-PLUS || MP-WEIXIN -->
		<view class="press-tips">点击输入行上边的名称可语音输入</view>
		
		<!-- #endif -->
		

		<view class="uni-list">

			<label-input v-for="(item, index) of (data.common && data.common.value)" ref="mpvuePicker" :key="index" :child_data="item" @input_value="inputValue"
			 :alldata="data.common.value" :inkey="index"></label-input>

			<!-- <label-input v-for="(item, index) of (data.bottom && data.bottom.value)" ref="mpvuePicker" :key="index" :datas="item" :alldata="data.common.value" :inkey="index"></label-input> -->

			<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-if="data.bottom && data.bottom.value" @click="goSampleTail()">
				<view class="uni-list-cell-navigate uni-navigate-right">
					<label>采样表尾数据</label>
				</view>
			</view>
			
			<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-if="data.cyd_info && data.cyd_info.value" @click="goPoint()">
				<view class="uni-list-cell-navigate uni-navigate-right">
					<label>监测点公共数据</label>
				</view>
			</view>

			<view class="uni-list-cell-divider" v-if="isShowSampleInfo">
				{{data.sample_info && data.sample_info.title}}
			</view>

			<view v-if="isShowSampleInfo">
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item, skey) of (data.sample_info && data.sample_info.value)"
				 :key="skey" @click="goSampleItem(skey)">
					<view class="uni-list-cell-navigate uni-navigate-right">
						<label>{{(item && item.code && item.code.value) || (data.sample_info && data.sample_info.title+mapList[skey])}}</label>
					</view>
				</view>
			</view>
			
			<view class="uni-list-cell" style="background-color: #f7f7f7;" v-if="isShowDay">
				采样天数
			</view>
			
			<view v-if="isShowDay">
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item, dayKey) of (data.sample_info && data.sample_info.value)"
				 :key="dayKey" @click="goDay(dayKey)">
					<view class="uni-list-cell-navigate uni-navigate-right">
						<label>第{{dayKey}}天</label>
					</view>
				</view>
			</view>

		</view>

		<!-- <view class="btn-row uni-flex"> -->
		<view class="bottom_btn">
			<button type="primary" class="primary uni-flex-item"  @tap="save">保存</button>
			<button type="primary" class="primary uni-flex-item" @tap="saveSubmit">保存并上传</button>
		</view>
	</view>
</template>

<script>
	import net from '../../net.js';
	import service from '../../service.js';
	import utils from '../../common/utils.js';
	import labelInput from '../../components/label-input.vue';
	import uploaddata from '../../uploaddata.js';
	import uniIcons from "@/components/uni-icons/uni-icons.vue"
	export default {
		components: {
			labelInput,
			uniIcons
		},
		// data: {
		// 	data: {
		// 		common: {}
		// 	},
		// 	codeId: 0,
		// 	mapList: {}, // 样品标题
		// 	dayList: {},
		// 	isShowSampleInfo: false,
		// 	isShowDay: false
		// },
		data(){
			return {
				data: {
						common: {}
					},
					codeId: 0,
					mapList: {}, // 样品标题
					dayList: {},
					isShowSampleInfo: false,
					isShowDay: false
			}
		},
		onLoad(res) {
			this.codeId = res.codeId;
			this.tableId = res.tableId;
			let data = service.getTaskDetailItem(this.codeId, this.tableId);
			
			Object.assign(this.data, data);
			// console.log(this.data,"1111")
			uni.setNavigationBarTitle({
				title: (this.data.common && this.data.common.title) || ''
			});
			
			if(this.data.sampling_info && this.data.sampling_info.type != 1) {
				this.isShowSampleInfo = false;
				this.countFun();
			}else {
				this.isShowDay = false;
				this.countDay();
			}
		},
		onReady() {
			this.initItem();
			console.log('onReady');
		},
		methods: {
			//组件传值事件,input组件传出的新值
			inputValue(newdata,key){
				console.log(newdata,key);
				this.data.common.value[key]=newdata;
			},
			// 样品列表
			goSampleItem(id) {
				let param = utils.toParam({codeId: this.codeId, tableId: this.tableId, id: id });
				uni.navigateTo({
					url: "./sampling_item" + param
				});
			},
			// 监测点公共数据
			goPoint() {
				let param = utils.toParam({codeId: this.codeId, tableId: this.tableId, field: 'cyd_info' });
				uni.navigateTo({
					url: "./sampling_item" + param
				});
			},
			// 跳到第几天
			goDay(dayNum){
				let param = utils.toParam({codeId: this.codeId, tableId: this.tableId, dayNum: dayNum});
				uni.navigateTo({
					url: "./sampling_daynum" + param
				});
			},
			// 表尾部数据
			goSampleTail() {
				let param = utils.toParam({ codeId: this.codeId, tableId: this.tableId });
				uni.navigateTo({
					url: "./sampling_tail" + param
				});
			},
			// 获取样品列表的编号
			countFun() {
				try{
					let itemList = this.data.sample_info.value;
					let map = Object.keys(itemList);
					map.forEach((item, index) => {
						if (itemList[item] != null) {
							this.mapList[item] = index + 1;
							this.isShowSampleInfo = true;
						}
					});
				}catch(e){
					//TODO handle the exception
				}
			},
			// 计算天数
			countDay() {
				try{
					let list = this.data.sample_info.value;
					for(let dayKey in list) {
						// 天数列表
						let dayData = list[dayKey].cyd_info;
						for(let pointKey in dayData) {
							// 采样点列表
							let pointData = dayData[pointKey];
							for(let sampleListKey in pointData) {
								// 样品列表
								if(pointData[sampleListKey] != null) {
									this.isShowDay = true;
									return;
								}
							}
						}
					}
				}catch(e){
					//TODO handle the exception
					console.log(e);
				}
			},
			// 初始化 需要填写的内容
			initItem() {
				setTimeout(() => {
					this.$refs.mpvuePicker && this.$refs.mpvuePicker.forEach((item, index) => {
						item.init();
					});
				}, 0);
			},
			save() {
				setTimeout(()=>{
					this.saveData();
				}, 500);
				// this.saveData();
			},
			saveSubmit() {
				setTimeout(()=>{
					 this.saveData(true)
				}, 500);
				// this.saveData(true);
			},
			saveData(isSubmit = false) {
				uni.showLoading({ title: '', mask: true });
				service.setTaskDetailItem(this.codeId, this.tableId, this.data, 'common');
				utils.exeMainCallBack({}, '上传');
				if (!isSubmit) {
					uni.hideLoading();
					uni.showToast({ icon: 'none', title: '保存成功' });
				} else {
					uploaddata.uploadSampleData(false, true);
				}
			}

		},
	}
</script>

<style scoped lang="scss">
	.index{
		width: 100%;
	}
	.bottom_btn{
	
		display: flex;	
		padding: 20rpx;
		button{
			margin: 0 10rpx;
		}
	}
	.hide {
		display: none;
	}

	.uni-list {
		margin: 10upx 0 60upx 0;
	}

	.uni-list-cell:after {
		left: 0;
	}

	.uni-list-cell {
		padding: 0 20upx;
		min-height: 90upx;
		color: #666;
	}

	.uni-list-cell.con-list {
		flex-wrap: wrap;
	}

	.uni-list-cell.con-list text {
		flex: 0 0 100%;
		font-size: 32upx;
	}

	.uni-list-cell-divider~.uni-list-cell {
		padding-left: 50upx;
		min-height: 70upx;
	}

	.uni-list-cell.no-left {
		padding-left: 20upx;
	}
</style>
